package br.com.blutufi;

/*adfafa
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ChatFrame extends JFrame {
	private static final long serialVersionUID = -95583138836547474L;

	private JLabel lbMessage;
	private JTextArea edtMessage;
	private JTextArea edtHistory;
	private JButton btSend;

	public static void centralize(Window window) {
		Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();

		int halfWidth = (screenDimension.width / 2);
		int halfHeight = (screenDimension.height / 2);

		int x = halfWidth - (window.getWidth() / 2);
		int y = halfHeight - (window.getHeight() / 2);
		int width = window.getWidth();
		int height = window.getHeight();

		window.setBounds(x, y, width, height);
	}

	public ChatFrame(String history) {
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initComponents(history);
		showGUI();
		centralize(this);

	}

	public void atualizaMessage(String message) {
		message = edtHistory.getText() + "\n" + message;
		edtHistory.setText(message);
	}

	private void initComponents(String history) {
		this.setLayout(new BorderLayout());

		this.lbMessage = new JLabel("Resposta: ");
		this.edtMessage = new JTextArea();
		this.edtHistory = new JTextArea(history);
		this.btSend = new JButton("Enviar");

		JPanel historyPanel = new JPanel();
		JPanel messagePanel = new JPanel();
		JPanel btPanel = new JPanel();
		btSend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				/*
				 * JOptionPane.showMessageDialog(null, "Texto a ser enviado: " +
				 * edtMessage.getText());
				 */
				Server.enviarMensagem(edtMessage.getText());

			}
		});

		JScrollPane messageScroll = new JScrollPane(edtMessage);
		JScrollPane historyScroll = new JScrollPane(edtHistory);

		btPanel.add(btSend);

		messagePanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// label
		c = new GridBagConstraints(0, 0, // gridx, gridy
				2, 1, // gridwidth, gridheight
				0.5, 0, // weightx, weighty,
				GridBagConstraints.NORTHWEST, // anchor
				GridBagConstraints.HORIZONTAL, // fill
				new Insets(0, 0, 0, 0), // insets
				0, 0 // ipadx, ipady
		);
		messagePanel.add(lbMessage, c);

		// message
		c = new GridBagConstraints(0, 1, // gridx, gridy
				1, 1, // gridwidth, gridheight
				1, 1, // weightx, weighty,
				GridBagConstraints.LAST_LINE_START, // anchor LAST_LINE_START
				GridBagConstraints.BOTH, // fill
				new Insets(0, 0, 0, 0), // insets
				0, 5 // ipadx, ipady
		);
		messagePanel.add(messageScroll, c);

		// button
		c = new GridBagConstraints(1, 1, // gridx, gridy
				1, 1, // gridwidth, gridheight
				0, 0, // weightx, weighty,
				GridBagConstraints.LAST_LINE_END, // anchor
				GridBagConstraints.NONE, // fill
				new Insets(10, 5, 10, 1), // insets
				0, 0 // ipadx, ipady
		);
		messagePanel.add(btPanel, c);

		historyPanel.setLayout(new GridLayout());
		historyPanel.add(historyScroll);

		this.add(historyPanel, BorderLayout.CENTER);
		this.add(messagePanel, BorderLayout.SOUTH);

		this.edtHistory.setEditable(false);

		// Dimension dimension1 = new Dimension(400, 150);
		// Dimension dimension2 = new Dimension(330, 130);
		Dimension windowDimension = new Dimension(400, 280);
		//
		// // this.btSend.setPreferredSize(dimension1);
		// // this.edtMessage.setPreferredSize(dimension2);
		//
		// historyScroll.setMinimumSize(dimension1);
		// historyScroll.setSize(dimension1);
		// historyScroll.setPreferredSize(dimension1);
		//
		this.setPreferredSize(windowDimension);
	}

	private void showGUI() {
		this.pack();
		this.setVisible(true);
		edtMessage.requestFocus();
	}

	public void iniciarServidor() {
		Server server = new Server();
		server.run();
		this.edtHistory.setEditable(true);
		edtHistory.setText("Servidor conectado!");
		this.edtHistory.setEditable(false);
	}

	/*
	 * public static void main(String[] args) { ChatFrame cf = new
	 * ChatFrame("Ta funfando?"); cf.iniciarServidor(); }
	 */
}